<?php

/**
 * Created by PhpStorm.
 * Script Name: MsgLog.php
 * Create: 2020/5/24 上午10:25
 * Description: 站点配置
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\admin\controller;


use app\common\model\MsgGatherGroup as GroupM;
use app\constants\Common;

class Msggathergroup extends Botbase
{
    /**
     * @var GroupM
     */
    protected $model;

    /**
     * @var array
     */
    private $tabList;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new GroupM();
        $this->tabList = [
            'index' => ['title' => '消息列表', 'href' => url('msglog/index')],
            'rule' => ['title' => '存档规则', 'href' => url('msglog/rule')],
            'group' => ['title' => '存档规则分组', 'href' => url('index')],
        ];
    }

    /**
     * 保存规则
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function index()
    {
        if (request()->isPost()) {
            $post_data = input('post.');
            $where = ['bot_id' => $this->bot['id']];
            $total = $this->model->total($where, true);
            if ($total) {
                $list = $this->model->getList([$post_data['page'], $post_data['limit']], $where, [], true, true);
            }else{
                $list = [];
            }
            $this->success('success', '', ['total' => $total, 'list' => $list]);
        }
        $builder = new ListBuilder();
        $builder->addTopButton('addnew')
            ->setTabNav($this->tabList, 'group')
            ->addTableColumn(['title' => 'ID', 'field' => 'id', 'minWidth' => 70])
            ->addTableColumn(['title' => '分组名称', 'field' => 'title', 'minWidth' => 100])
            ->addTableColumn(['title' => '创建时间', 'field' => 'create_time', 'type' => 'datetime', 'minWidth' => 180])
            ->addTableColumn(['title' => '操作', 'minWidth' => 100, 'type' => 'toolbar'])
            ->addRightButton('edit')
            ->addRightButton('delete');
        return $builder->show();
    }

    public function add(){
        $data = ['bot_id' => $this->bot['id']];
        $builder = new FormBuilder();
        $builder->setPostUrl(url('savePost'))
            ->addFormItem('bot_id', 'hidden', 'bot_id', 'bot_id')
            ->addFormItem('title', 'text', '分组名称', '分组名称',[], 'maxlength=150')
            ->setFormData($data);
        return $builder->show();
    }

    public function edit(){
        $id = input('id', null);
        $data = $this->model->getOneByMap([
            'bot_id' => $this->bot['id'],
            'id' => $id
        ], true, true);

        if (!$data) {
            $this->error('参数错误');
        }

        $builder = new FormBuilder();
        $builder->setPostUrl(url('savePost'))
            ->addFormItem('id', 'hidden', 'id', 'id')
            ->addFormItem('title', 'text', '分组名称', '分组名称',[], 'maxlength=150')
            ->setFormData($data);
        return $builder->show();
    }
}